package cmd

import (
	"fmt"

	"gitee.com/shangc1016/libscanner/front"
	"gitee.com/shangc1016/libscanner/tools"
	"github.com/spf13/cobra"
)

var rootCmd *cobra.Command
var scanCmd *cobra.Command

var (
	outputPath string
	outputType string
)

func rootInit() {
	rootCmd = &cobra.Command{
		Use: "libscanner",
	}
}

func ScanInit() {
	scanCmd = &cobra.Command{
		Use:   "scan",
		Short: "scan a project",
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) != 1 {
				fmt.Printf("specific one project path\n")
				return
			}
			if !tools.IsDir(args[0]) {
				fmt.Println("wrong project path")
				return
			}
			projectPath := args[0]
			front.Scan(projectPath, outputPath, outputType)
		},
	}
	// 在指定了outputPath的时候，outputType才生效
	// 不指定outputPath，输出到cli
	scanCmd.Flags().StringVar(&outputPath, "outputPath", "", "specific report path")
	scanCmd.Flags().StringVar(&outputType, "outputType", "stdout", "specific output type")
}

func init() {
	rootInit()
	ScanInit()

	rootCmd.AddCommand(scanCmd)
}

func NewCmd() *cobra.Command {
	return rootCmd
}
